const mongoose = require('mongoose');
const User = require('../models/User');
require('dotenv').config();

const testUser = {
  username: 'test',
  email: 'test@example.com',
  password: 'test123',
  role: 'user',
  points: 0,
  carbonSaved: 0,
  preferences: {
    notifications: {
      email: true,
      push: true
    },
    privacy: {
      showProfile: true,
      showProgress: true
    }
  }
};

async function createTestUser() {
  try {
    await mongoose.connect(process.env.MONGODB_URI);
    console.log('连接到MongoDB成功');

    // 检查用户是否已存在
    const existingUser = await User.findOne({ email: testUser.email });
    if (existingUser) {
      console.log('测试用户已存在');
      return;
    }

    // 创建新用户
    const user = new User(testUser);
    await user.save();
    console.log('测试用户创建成功:', {
      username: user.username,
      email: user.email,
      _id: user._id
    });

  } catch (error) {
    console.error('创建测试用户失败:', error);
  } finally {
    await mongoose.connection.close();
  }
}

createTestUser(); 